<template>
    <div>
        <div class="operate center-flex-box">
            <el-button class="item" type="primary" v-if="hasPermission('ADD_AREA')" @click="showEditor(null)">新增
            </el-button>
            <el-input placeholder="请输入内容" v-model="queryInfo.content" class="input-with-select item">
                <el-select v-model="queryInfo.mode" clearable slot="prepend" placeholder="请选择">
                    <el-option label="ID" value="ids"></el-option>
                    <el-option label="名称" value="name"></el-option>
                </el-select>
            </el-input>
            <el-button class="item" @click="obtainAreas">查找</el-button>
        </div>
        <el-table
            :data="areas"
            stripe
            max-height="450"
            style="width: 100%">
            <el-table-column
                prop="id"
                label="ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="name"
                label="名称"
                sortable
                width="100">
            </el-table-column>
            <el-table-column
                prop="description"
                label="描述"
                width="240">
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="attentionCount"
                label="关注数量"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="itemCount"
                label="项数量"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                v-if="hasPermission('UPDATE_AREA')||hasPermission('DELETE_AREA')"
                fixed="right"
                width="180"
                label="操作">
                <template slot-scope="scope">
                    <el-button @click="showEditor(scope.row.id)"
                               v-if="hasPermission('UPDATE_AREA')"
                               type="primary"
                               size="mini"
                               plain>
                        编辑
                    </el-button>
                    <el-button @click="remove(scope.row.id)"
                               v-if="hasPermission('DELETE_AREA')"
                               type="danger"
                               size="mini"
                               plain>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="operate==='insert'?'新增区域':'修改区域'" :visible.sync="editor" width="40%">
            <el-form ref="area" :rules="rules" :model="area" label-position="left" label-width="80px" size="small">
                <el-form-item label="区域ID" prop="id">
                    <el-input v-model="area.id" disabled></el-input>
                </el-form-item>
                <el-form-item label="区域名称" prop="name">
                    <el-input v-model="area.name"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-select class="left" v-model="area.status" placeholder="请选择">
                        <el-option label="正常" value="0"></el-option>
                        <el-option label="准备删除" value="1"></el-option>
                        <el-option label="不要关注" value="2"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="area.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editor = false">取 消</el-button>
                <el-button type="primary" @click="commit">提交</el-button>
            </div>
        </el-dialog>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.page"
            :page-size="pageInfo.size"
            layout="total, prev, pager, next, jumper"
            :total="pageInfo.total">
        </el-pagination>
    </div>
</template>

<script>
    import { mapGetters, mapActions } from 'vuex'

    export default {
        name: 'areaOpt',
        data () {
            return {
                areas: [],
                area: { id: 'ID会在添加时自动生成', status: '0' },
                pageInfo: {
                    page: 1,
                    size: 100,
                    total: 0
                },
                queryInfo: {
                    mode: '',
                    content: ''
                },
                editor: false,
                operate: 'insert',
                rules: {
                    id: [{ required: true, message: '请输入ID', trigger: 'blur' }],
                    name: [{ required: true, message: '请输入区域名称', trigger: 'blur' }]
                }
            }
        },
        mounted () {
            this.obtainAreas()
        },
        computed: {
            ...mapGetters({
                hasPermission: 'user/hasPermission'
            })
        },
        methods: {
            ...mapActions({
                pageAreaAction: 'area/pageArea',
                pageAreaByIdsAction: 'area/pageAreaByIds',
                pageAreaByNameAction: 'area/pageAreaByName',
                insertAreaAction: 'area/insertArea',
                updateAreaAction: 'area/updateArea',
                deleteAreaAction: 'area/deleteArea'
            }),
            handleSizeChange (val) {
                this.pageInfo.size = val
                this.obtainAreas()
            },
            handleCurrentChange (val) {
                this.pageInfo.page = val
                this.obtainAreas()
            },
            obtainAreas () {
                let action = this.pageAreaAction
                switch (this.queryInfo.mode) {
                    case 'ids':
                        action = this.pageAreaByIdsAction
                        break
                    case 'name':
                        action = this.pageAreaByNameAction
                        break
                }
                this.action(action(this.obtainQuery()), data => {
                    if (data.data) {
                        this.areas = data.data.records
                        this.areas.forEach(area => {
                            switch (area.status) {
                                case 0:
                                    area.status = '正常'
                                    break
                                case 1:
                                    area.status = '准备删除'
                                    break
                                case 2:
                                    area.status = '不要关注'
                                    break
                            }
                        })
                        this.pageInfo.total = data.data.total
                    } else {
                        this.areas = []
                    }
                })
            },
            obtainQuery () {
                let query = {
                    page: this.pageInfo.page,
                    size: this.pageInfo.size
                }
                query[this.queryInfo.mode] = this.queryInfo.content
                return query
            },
            showEditor (id) {
                if (id) {
                    this.area = { ...this.areas.find(area => area.id === id) }
                    switch (this.area.status) {
                        case '正常':
                            this.area.status = '0'
                            break
                        case '准备删除':
                            this.area.status = '1'
                            break
                        case '不要关注':
                            this.area.status = '2'
                            break
                    }
                    this.operate = 'update'
                } else {
                    this.area = { id: 'ID会在添加时自动生成', status: '0' }
                    this.operate = 'insert'
                }
                this.editor = true
            },
            commit () {
                this.$refs['area'].validate((valid) => {
                    if (valid) {
                        if (this.operate === 'insert') {
                            this.action(this.insertAreaAction(this.area), data => {
                                this.$message.success('添加成功')
                                this.editor = false
                                this.obtainAreas()
                            })
                        } else if (this.operate === 'update') {
                            this.action(this.updateAreaAction(this.area), data => {
                                this.$message.success('更新成功')
                                this.editor = false
                                this.obtainAreas()
                            })
                        }
                    } else {
                        return false
                    }
                })
            },
            remove (id) {
                this.action(this.deleteAreaAction(id), data => {
                    this.$message.success('删除成功')
                    this.obtainAreas()
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import '../../../../../style/scss';

    .operate {
        height: 60px;
        width: 90%;
        padding: 0 5%;

        .item {
            margin: 24px;
        }

        .input-with-select {
            width: 360px;
            background-color: #fff;

            .el-select {
                width: 100px;
            }
        }
    }

    .left {
        float: left;
    }
</style>
